package com.chtw.vuedemo.controller;

import com.chtw.vuedemo.bean.News;
import com.chtw.vuedemo.response.RespUtil;
import com.chtw.vuedemo.response.enumutil.CodeAndMsg;
import com.chtw.vuedemo.service.impl.NewsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author chtw
 * @date 2020-10-04 14:20
 */
@RequestMapping("/news")
@RestController
public class NewsController {

    @Autowired
    private NewsServiceImpl newsService;

    @GetMapping(value = "/list")
    public RespUtil<List<News>> list(){
        try {
            return new RespUtil<>(CodeAndMsg.SUCCESS, newsService.list());
        }catch (Exception e){
            return new RespUtil<>(CodeAndMsg.FAIL, null);
        }
    }

    @GetMapping(value = "/get/{id}")
    public RespUtil<News> get(@PathVariable int id){
        try {
            return new RespUtil<>(CodeAndMsg.SUCCESS, newsService.getById(id));
        }catch (Exception e){
            return new RespUtil<>(CodeAndMsg.FAIL, null);
        }
    }
}
